import {Injectable} from "@angular/core";
import {Http, Headers, RequestOptions} from "@angular/http";
import 'rxjs/add/operator/map';
import {Observable} from "rxjs";

@Injectable()
export class BaseService {
  // public HOST_URL = 'http://wangyi.butterfly.mopaasapp.com/news/api?';

  public HOST_URL = 'http://v.juhe.cn/toutiao/index';

  public XWLIST_URL = 'http://wangyi.butterfly.mopaasapp.com/detail/api?simpleId=';


  constructor(public http: Http) {
  }

  public getWithoutData(url): Observable<any> {
    let headers = new Headers();
    // headers.append('Content-Type', 'application/json');
    let options = new RequestOptions({headers: headers});
    return this.http.get(this.HOST_URL + url, options).map(res => res.json());
  }

  // public get(url, dataString): Observable<any> { //千鸟版get url后面加了 '?token=&[object%20Object'
  //   let token = localStorage['token'] || '';
  //   let headers = new Headers();
  //   // headers.append('Content-Type', 'application/json');
  //   let options = new RequestOptions({headers: headers});
  //   return this.http.get(this.HOST_URL + url + "?token=" + token + "&" + dataString, options).map(res => res.json());
  // }

  public get(url, dataString): Observable<any> {
    let headers = new Headers({});
    let options = new RequestOptions({headers: headers});
    return this.http.get(this.HOST_URL + url , options).map(res => res.json());
  }

  public getList(url, dataString): Observable<any> {
    let headers = new Headers();
    let options = new RequestOptions({headers: headers});
    return this.http.get(this.XWLIST_URL + url , options).map(res => res.json());
  }

  public getWithoutToken(url, dataString): Observable<any> {
    let headers = new Headers();
    // headers.append('Content-Type', 'application/json');
    let options = new RequestOptions({headers: headers});
    return this.http.get(this.HOST_URL + url + "?" + dataString, options).map(res => res.json());
  }

  public post(url, body): Observable<any> {
    body['token'] = localStorage['token'];
    let headers = new Headers({'Content-Type': 'application/json'});
    let options = new RequestOptions({headers: headers});
    return this.http.post(this.HOST_URL + url, body, options).map(res => res.json());
  }
}
